package com.meitianhui.convenitenbuy.module.store.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.store.model.OneLotteryParticipateBean;
import com.meitianhui.convenitenbuy.push.model.OneLotteryPushBean;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.RxBus;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/22.
 */

public class OneLotteryParticipateAdapter extends BaseQuickAdapter<OneLotteryParticipateBean.ListEntity, BaseViewHolder> {
    private Context mContext;
    private Map<String, String> mUrlMap;

    public OneLotteryParticipateAdapter(Context context, @Nullable List<OneLotteryParticipateBean.ListEntity> data, Map<String, String> urlMap) {
        super(R.layout.item_store_one_lottery_participate, data);
        mData = data;
        mContext = context;
        mUrlMap = urlMap;
    }

    @Override
    protected void convert(BaseViewHolder helper, OneLotteryParticipateBean.ListEntity item) {
        GlideUtils.setRectImage(mContext,mUrlMap.get(item.getAward_pic_path1()), R.mipmap.icon_default_app, R.mipmap.icon_default_app, helper.getView(R.id.iv_one_lottery_participate_img));
        String type = item.getActivity_type();
        TextView tvType = helper.getView(R.id.tv_one_lottery_participate_type);
        if (TextUtils.equals(type, "YYY")) {
            tvType.setText(mContext.getString(R.string.home_one_lottery_participate_type1));
        } else if (TextUtils.equals(type, "GGL")) {
            tvType.setText(mContext.getString(R.string.home_one_lottery_participate_type2));
        } else if (TextUtils.equals(type, "DSK")) {
            tvType.setText(mContext.getString(R.string.home_one_lottery_participate_type3));
        }
        helper.setText(R.id.tv_one_lottery_participate_name, item.getAward_name());
        helper.setText(R.id.tv_one_lottery_participate_title, item.getTitle());
        helper.setText(R.id.tv_one_lottery_participate_time, "参与时间：" + item.getCreated_date());
        helper.setText(R.id.tv_one_lottery_participate_store_name, item.getStores_name());
        String status = item.getStatus();
        ImageView ivStatus = helper.getView(R.id.iv_one_lottery_participate_status);
        TextView tvStatus = helper.getView(R.id.tv_one_lottery_participate_status);
        RelativeLayout rlWin = helper.getView(R.id.rl_one_lottery_participate_status);
        if (TextUtils.equals(status, "processing")) {
            ivStatus.setVisibility(View.GONE);
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(mContext.getString(R.string.home_one_lottery_participate_status_processing));
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.red));
            rlWin.setVisibility(View.GONE);
        } else if (TextUtils.equals(status, "win")) {
            ivStatus.setVisibility(View.VISIBLE);
            tvStatus.setVisibility(View.GONE);
            helper.setVisible(R.id.iv_one_lottery_participate_code, true);
            rlWin.setVisibility(View.VISIBLE);
            rlWin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("activity_type", item.getActivity_type());
                        jsonObject.put("stores_id", item.getStores_id());
                        jsonObject.put("activity_id", item.getActivity_id());
                        jsonObject.put("consumer_id", item.getConsumer_id());
                        jsonObject.put("process_id", item.getProcess_id());
                        jsonObject.put("draw_code", item.getDraw_code());
                        OneLotteryPushBean bean = new OneLotteryPushBean(0, item.getProcess_id(), jsonObject.toString());
                        bean.setPosition(helper.getPosition());
                        RxBus.get().post(bean);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (TextUtils.equals(status, "miss")) {
            ivStatus.setVisibility(View.GONE);
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(mContext.getString(R.string.home_one_lottery_participate_status_miss));
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.textColorWeak));
            rlWin.setVisibility(View.GONE);
        } else if (TextUtils.equals(status, "exchanged")) {
            ivStatus.setVisibility(View.GONE);
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(mContext.getString(R.string.home_one_lottery_participate_status_exchanged));
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.textColorWeak));
            rlWin.setVisibility(View.GONE);
        } else if (TextUtils.equals(status, "refunded")) {
            ivStatus.setVisibility(View.GONE);
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(mContext.getString(R.string.home_one_lottery_participate_status_refunded));
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.textColorWeak));
            rlWin.setVisibility(View.GONE);
        }

    }

    public void setNewData(List data, Map<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }

    public void addNewData(List data, Map<String, String> map) {
        addData(data);
        this.mUrlMap = map;
    }

}
